package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.FundAccountMapper;
import com.example.demo.mapper.UserCompetitionMapper;
import com.example.demo.mapper.UserFundAccountMapper;
import com.example.demo.pojo.Accounts.FundAccounts;
import com.example.demo.pojo.Accounts.UserFundAccounts;
import com.example.demo.pojo.R;
import com.example.demo.service.FundAccountService;
import com.example.demo.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class FundAccountServiceImpl extends ServiceImpl<FundAccountMapper, FundAccounts> implements FundAccountService {

    @Resource
    private FundAccountMapper fundAccountMapper;

    @Resource
    private UserFundAccountMapper userFundAccountMapper;

    @Resource
    private UserUtil userUtil;

    @Resource
    private UserCompetitionMapper userCompetitionMapper;

    @Override
    @Transactional
    public R<Map<String, Object>> getAllFundAccountsList(int id, int pageNum, int pageSize) {
        Page<FundAccounts> page = new Page<>(pageNum, pageSize);
        QueryWrapper<FundAccounts> fundAccountsQueryWrapper = new QueryWrapper<>();
        fundAccountsQueryWrapper.eq("account_id", id);
        IPage<FundAccounts> fundAccountsIPage = fundAccountMapper.selectPage(page, fundAccountsQueryWrapper);
        List<FundAccounts> fundAccountsList = fundAccountsIPage.getRecords();
        long totalCount = fundAccountsIPage.getTotal();
        // 记录日志
        log.info("总共找到账户记录数：{}", totalCount);
        log.info("当前页码：{}", page.getCurrent());
        log.info("总页数：{}", page.getPages());

        Map<String, Object> result = new HashMap<>();
        result.put("fundAccounts", fundAccountsList);
        result.put("totalCount", totalCount);

        return R.success(result);
    }

    @Override
    public R<Map<String, Object>> getAllUserFundAccountsList(int id, int pageNum, int pageSize) {
        // 创建 Page 对象，设置当前页码和每页显示的数量
        Page<UserFundAccounts> page = new Page<>(pageNum, pageSize);
        // 创建 QueryWrapper，设置查询条件
        QueryWrapper<UserFundAccounts> userFundAccountsQueryWrapper = new QueryWrapper<>();
        userFundAccountsQueryWrapper.eq("user_account_id", id);
        // 执行分页查询
        IPage<UserFundAccounts> userFundAccountsIPage = userFundAccountMapper.selectPage(page, userFundAccountsQueryWrapper);
        // 获取查询结果
        List<UserFundAccounts> userFundAccounts = userFundAccountsIPage.getRecords();
        long totalCount = userFundAccountsIPage.getTotal();
        // 使用 Log 记录日志
        log.info("总共找到用户账户记录数：{}", totalCount);
        log.info("当前页码：{}", page.getCurrent());
        log.info("总页数：{}", page.getPages());

        // 准备返回的结果
        Map<String, Object> result = new HashMap<>();
        result.put("userFundAccounts", userFundAccounts);
        result.put("totalCount", totalCount);

        return R.success(result);
    }

}
